package com.yjz.study.arouter

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import com.alibaba.android.arouter.launcher.ARouter
import com.yjz.common.util.AppGlobals

/**
 *作用 : ARouter相关逻辑处理
 *作者 : yjz
 *时间 : 2025/2/13 16:01
 */
object ARouterLogic {

    class Path {
        companion object {
            // 登陆页面
            const val LOGIN_PATH = "/login/activity"

            // 注册页面
            const val REGISTER_PATH = "/login/register/activity"

            // 商品列表
            const val GOOD_LIST = "/good/list/activity"

            // 商品详情
            const val GOOD_DETAIL = "/good/detail/activity"

            // 我的课程通知
            const val NOTICE = "/course/notice/activity"

            // 统计降级错误页
            const val DEGRADE_GLOBAL_PATH = "/degrade/global/activity"
        }
    }

    class Interceptor {
        companion object {
            // 页面降级拦截器
            const val BIZ = "/biz/interceptor"
        }
    }

    class Service {
        companion object {
            // 页面降级service
            const val DEGRADE = "/degrade/service"
        }
    }

    fun navigation(path: String) {
        ARouter.getInstance()
            .build(path)
            .navigation()
    }

    fun navigation(path: String, params: HashMap<String, String>) {
        val build = ARouter.getInstance().build(path)
        params.forEach { (key, value) ->
            build.withString(key, value)
        }
        build.navigation()
    }

    fun navigation(path: String, bundle: Bundle) {
        val build = ARouter.getInstance().build(path)
        build.with(bundle)
        build.navigation()
    }

    // 拉起浏览器
    fun startActivity4Browser(url: String) {
        val uri = Uri.parse(url)
        val intent = Intent(Intent.ACTION_VIEW, uri)
        //这个目的是为了 防止在部分机型上面 拉不起浏览器，，比说华为
        intent.addCategory(Intent.CATEGORY_BROWSABLE)
        // 是为了 使用Application  context 启动activity 不会报错
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        AppGlobals.get()?.startActivity(intent)
    }

    fun inject(target: Any) {
        ARouter.getInstance().inject(target)
    }
}